//
//  LXSelectNumberCell.m
//  TestDemo
//
//  Created by xiao.zhao@lachesis-mh.com on 16/2/23.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXSelectNumberCell.h"
#import "LXSelectNumberView.h"

@interface LXSelectNumberCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) LXSelectNumberView *selNumView;
@property (nonatomic, strong) UILabel *numberLabel;

@end

@implementation LXSelectNumberCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)setup
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    _containerView = [[UIView alloc]init];
    _containerView.layer.borderWidth = 0.5f;
    _containerView.layer.borderColor = [UIColor colorWithHexString:@"d1d1d1"].CGColor;
    [self.contentView addSubview:_containerView];
    
    _headImage = [[UIImageView alloc]init];
    [_containerView addSubview:_headImage];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.font = [UIFont lx_systemFontOfSize:21.0f];
    _nameLabel.textColor = kAppNormalTextColor;
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    _nameLabel.numberOfLines = 2;
    _nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _nameLabel.adjustsFontSizeToFitWidth = YES;
    [_containerView addSubview:_nameLabel];
    
    _numberLabel = [[UILabel alloc]init];
    _numberLabel.font = [UIFont lx_systemFontOfSize:21.0f];
    _numberLabel.adjustsFontSizeToFitWidth = YES;
    _numberLabel.textColor = kAppThemeColor;
    _numberLabel.textAlignment = NSTextAlignmentRight;
    [_containerView addSubview:_numberLabel];
    
    _selNumView = [[LXSelectNumberView alloc]init];
    [self.contentView addSubview:_selNumView];
    [_selNumView addObserver:self forKeyPath:@"quantity" options:NSKeyValueObservingOptionNew context:NULL];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self).offset(0.5f);
        make.right.equalTo(self).offset(0.5);
    }];
    
    [_headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView.mas_left).offset([LXAutoLength lx_L:20]);
        make.centerY.equalTo(self.containerView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:30], [LXAutoLength lx_L:30]));
    }];
    
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView.mas_top).with.offset([LXAutoLength lx_L:10]);
        make.centerY.equalTo(self.containerView.mas_centerY);
        make.right.equalTo(_selNumView.mas_left).with.offset([LXAutoLength lx_L:-15]);
        make.width.mas_equalTo([LXAutoLength lx_L:80]);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView.mas_top).with.offset(0.0f);
        make.centerY.equalTo(self.containerView.mas_centerY);
        make.left.equalTo(_headImage.mas_right).with.offset([LXAutoLength lx_L:10]);
        make.right.equalTo(_numberLabel.mas_left).with.offset([LXAutoLength lx_L:-15]);
    }];
    
    [_selNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView.mas_top).with.offset([LXAutoLength lx_L:15]);
        make.centerY.equalTo(self.containerView.mas_centerY);
        make.width.mas_equalTo([LXAutoLength lx_L:120]);
        make.right.equalTo(_containerView.mas_right).offset([LXAutoLength lx_L:-15]);
    }];
}

- (void)setMaxSelect:(NSUInteger)maxSelect
{
    _maxSelect = maxSelect;
    _selNumView.maxSelect = maxSelect;
}

-(void)setItem:(LXSelectNumberItem *)item
{
    if(_item != item && item)
    {
        _item = item;
        
        [self.headImage setImage:_item.image];
        [self.nameLabel setText:_item.text];
        [self.selNumView setUnit:_item.unit];
        self.selNumView.unitName = _item.unitAfter;
        [self.selNumView setQuantity:_item.number / _item.unit];
        
        if (!_item.image) {
            [_headImage mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(0);
            }];
            [_nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_headImage.mas_right).with.offset(0.0f);
            }];
        }
    }
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if([keyPath isEqualToString:@"quantity"])
    {
        CGFloat newValue = [[change objectForKey:NSKeyValueChangeNewKey] floatValue];
        _item.number = newValue * _item.unit;
        CGFloat newUnit = _item.unit == 0 ? 1 : _item.unit;
        _numberLabel.text = [NSString stringWithFormat:@"%.1f%@", newValue * newUnit, _item.unitAfter];
        if (_selectNumberDelegate && [_selectNumberDelegate respondsToSelector:@selector(LXSelectNumberCell:withValue:)]) {
            [_selectNumberDelegate LXSelectNumberCell:self withValue:newValue];
        }
    }
}

- (void)dealloc
{
    [_selNumView removeObserver:self forKeyPath:@"quantity"];
}
@end
